package cn.sykj.car.service.impl;

import cn.sykj.car.dict.ConfigRechargeOverflyFlagEnum;
import cn.sykj.car.dict.ConfigRechargeTypeEnum;
import cn.sykj.car.common.Const;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.ConfigRecharge;
import cn.sykj.car.exception.CommonException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.ConfigRechargeMapper;
import cn.sykj.car.service.IConfigRechargeService;
import cn.sykj.car.utils.DiffEntityUtils;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充值设置 服务实现类
 * </p>
 *
 * @author xuming
 * @since 2021-06-25
 */
@Service
public class ConfigRechargeServiceImpl extends ServiceImpl<ConfigRechargeMapper, ConfigRecharge> implements IConfigRechargeService {

    /**
     * 添加充值设置
     *
     * @param configRecharge
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO addConfigRecharge(ConfigRecharge configRecharge) throws Exception {
        // 判断参数是否为空
        if (Utils.isEmpty(configRecharge)) {
            throw new ParameterInvalidException();
        }

        // 判断储值类型
        if(Utils.isEmpty(configRecharge.getType())){
            throw new CommonException("","储值类型不能为空！");
        }

        // 判断，如果储值类型为1-固定金额，则rechargeAmount必填
        if(configRecharge.getType().intValue() == ConfigRechargeTypeEnum.FIXED_RECHARGE_AMOUNT.VALUE){
            if(Utils.isEmpty(configRecharge.getRechargeAmount())
                    || configRecharge.getRechargeAmount() <= 0){
                throw new CommonException("","当储值类型为固定金额时，充值金额必填且必须大于0元！");
            }
        }
        if(configRecharge.getType().intValue() == ConfigRechargeTypeEnum.CYCLE__RECHARGE_AMOUNT.VALUE){
            if(Utils.isEmpty(configRecharge.getRechargeAmount())
                    || configRecharge.getRechargeAmount() <= 0){
                // 当储值类型为无固定金额时，最小值赋值为0
                configRecharge.setRechargeAmount(0);
            }
        }

        // 当选择无固定金额充值时，选择循环赠送，则在充值时会循环赠送积分、返现或者赠送金额；否则无赠送，所以必填
        // 当选择固定金额充值时，无论填或者不填循环or不循环，都认为是不循环赠送
        if(configRecharge.getType().intValue() == ConfigRechargeTypeEnum.CYCLE__RECHARGE_AMOUNT.VALUE ){
            if(Utils.isEmpty(configRecharge.getOverflyFlag())){
                throw new CommonException("","当储值类型选择无固定金额充值时，是否循环赠送必填！");
            }
        }else{
            configRecharge.setOverflyFlag(ConfigRechargeOverflyFlagEnum.NOT_RECHARGE_SEND_AMOUNT.VALUE);
        }

        if(Utils.isEmpty(configRecharge.getRechargeSendAmount())||configRecharge.getRechargeSendAmount() <= 0){
            configRecharge.setRechargeSendAmount(0);
        }
        if(Utils.isEmpty(configRecharge.getSendCash())||configRecharge.getSendCash() <= 0){
            configRecharge.setSendCash(0);
        }
        if(Utils.isEmpty(configRecharge.getSendIntegral())||configRecharge.getSendIntegral() <= 0){
            configRecharge.setSendIntegral(0);
        }

        boolean ret = this.save(configRecharge);

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("添加充值设置");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("添加充值设置，ID为：" + configRecharge.getId());
        return responseMessageVO;
    }

    /**
     * 删除充值设置
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO deleteConfigRecharge(Integer id) throws Exception {

        //判断参数是否为空
        if (Utils.isEmpty(id)) {
            throw new ParameterInvalidException();
        }

        // 判断参数是否存在
        ConfigRecharge configRecharge = this.getById(id);
        if (Utils.isEmpty(configRecharge)) {
            throw new CommonException(StatusCode.CONFIG_RECHARGE_NOT_FOUND.getCode(),
                    StatusCode.CONFIG_RECHARGE_NOT_FOUND.getMsg());
        }

        boolean ret = this.removeById(id);
        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType(" 删除充值设置");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("   删除充值设置，ID为:" + configRecharge.getId() +
                ";" + " 删除充值设置:" + configRecharge.getType());
        return responseMessageVO;
    }

    /**
     * 修改充值设置信息
     *
     * @param configRecharge
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO updateConfigRecharge(ConfigRecharge configRecharge) throws Exception {

        // 验证参数是否为空
        if (Utils.isEmpty(configRecharge) || Utils.isEmpty(configRecharge.getId())) {
            throw new ParameterInvalidException();
        }

        // 判断原始记录是否存在
        ConfigRecharge o_configRecharge = this.getById(configRecharge.getId());
        if (Utils.isEmpty(o_configRecharge) || o_configRecharge.getDelFlag().equals(Const.DEL_FLAG_TRUE)) {
            throw new CommonException("", "记录不存在");
        }

        // 判断储值类型
        if(Utils.isEmpty(configRecharge.getType())){
            throw new CommonException("","储值类型不能为空！");
        }

        // 判断，如果储值类型为1-固定金额，则rechargeAmount必填
        if(configRecharge.getType().intValue() == ConfigRechargeTypeEnum.FIXED_RECHARGE_AMOUNT.VALUE){
            if(Utils.isEmpty(configRecharge.getRechargeAmount())
                    || configRecharge.getRechargeAmount() <= 0){
                throw new CommonException("","当储值类型为固定金额时，充值金额必填且必须大于0元！");
            }
        }
        if(configRecharge.getType().intValue() == ConfigRechargeTypeEnum.CYCLE__RECHARGE_AMOUNT.VALUE){
            if(Utils.isEmpty(configRecharge.getRechargeAmount())
                    || configRecharge.getRechargeAmount() <= 0){
                // 当储值类型为无固定金额时，最小值赋值为0
                configRecharge.setRechargeAmount(0);
            }
        }

        // 当选择无固定金额充值时，选择循环赠送，则在充值时会循环赠送积分、返现或者赠送金额；否则无赠送，所以必填
        // 当选择固定金额充值时，无论填或者不填循环or不循环，都认为是不循环赠送
        if(configRecharge.getType().intValue() == ConfigRechargeTypeEnum.CYCLE__RECHARGE_AMOUNT.VALUE ){
            if(Utils.isEmpty(configRecharge.getOverflyFlag())){
                throw new CommonException("","当储值类型选择无固定金额充值时，是否循环赠送必填！");
            }
        }else{
            configRecharge.setOverflyFlag(ConfigRechargeOverflyFlagEnum.NOT_RECHARGE_SEND_AMOUNT.VALUE);
        }

        if(Utils.isEmpty(configRecharge.getRechargeSendAmount())||configRecharge.getRechargeSendAmount() <= 0){
            configRecharge.setRechargeSendAmount(0);
        }
        if(Utils.isEmpty(configRecharge.getSendCash())||configRecharge.getSendCash() <= 0){
            configRecharge.setSendCash(0);
        }
        if(Utils.isEmpty(configRecharge.getSendIntegral())||configRecharge.getSendIntegral() <= 0){
            configRecharge.setSendIntegral(0);
        }

        // 把要更新的对象中的属性，覆盖到已有对象中
        boolean ret = baseMapper.updateConfigRecharge(configRecharge) > 0;

        // 定义修改字符串：
        String editMes = "";

        // 遍历修改的内容与原始内容相比较，如果有变更，则记录
        ConfigRecharge afterConfigRecharge = this.getById(configRecharge.getId());
        Map<String, List<Object>> diffMap = DiffEntityUtils.getDifferent(o_configRecharge, afterConfigRecharge);
        if (Utils.notEmpty(diffMap) && diffMap.size() > 0) {
            for (String s : diffMap.keySet()) {
                editMes = editMes + ">" + s + "==>修改前【" + diffMap.get(s).get(0) + "】-修改后【" + diffMap.get(s).get(1) + "】";
            }
        }

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("修改充值设置");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("修改充值设置，ID为：" + afterConfigRecharge.getId() + ";具体修改信息为：" + editMes);
        return responseMessageVO;
    }
}
